package com.weed.helper;

import javax.naming.*;

import javax.rmi.*;
import java.lang.reflect.Method;

import java.util.*;
import javax.sql.RowSet;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.*;
import java.math.BigDecimal;
import com.weed.core.CommonInput;
import com.weed.core.CommonOutput;

/**
 *
 * <p>Title: EJB动态代理</p>
 * <p>Description: 兼容多种J2EE容器的部署</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author Jason zhuo
 * @version 1.0
 */
public class EJBAgentHelper {

  static private String EJB_DISPATCH_NAME="daoDispatch";

  private Object remoteObject = null;
  private String ejbName = "";
  private Context ctx;

  public EJBAgentHelper(String jndiName) throws Exception {
//    try {
      //获取缓存
      //remoteObject = CacheUtil.get(jndiName);
      //System.out.print(remoteObject);
      //if (remoteObject == null) {
//        Hashtable ht = EJBConfigHelper.LoadXML();
//        EJBConfigHelper ejb_config = (EJBConfigHelper) ht.get(jndiName);
//        ctx=ejb_config.getCONTEXT();
//        String DEPLOY_TYPE=ejb_config.getDEPLOY_TYPE();
//
//        if(DEPLOY_TYPE.equals("local")){
//            Object home = ctx.lookup(ejb_config.getLOCAL_EJB_JNDI_PREFIX()+jndiName+ejb_config.getLOCAL_EJB_JNDI_SUFFIX());
//            EJBLocalHome ejbHome = (EJBLocalHome) PortableRemoteObject.narrow(home,EJBLocalHome.class);
//            Class ejbHomeClass = ejbHome.getClass();
//            Method method = ejbHomeClass.getDeclaredMethod("create",null);
//            remoteObject = method.invoke(ejbHome, null);
//        }else{
//           Object home = ctx.lookup(ejb_config.getEJB_JNDI_PREFIX()+jndiName);
//           EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(home, EJBHome.class);
//           Class ejbHomeClass = ejbHome.getClass();
//           Method method = ejbHomeClass.getDeclaredMethod("create", null);
//           remoteObject = method.invoke(ejbHome, null);
//           System.out.println("remote");
//        }
//        //缓存
//        //CacheUtil.put(jndiName, remoteObject);
//      //}
//      ejbName = jndiName;
//    }
//    catch (Exception e) {
//      throw new Exception(e.getMessage());
//    }
  }

  /**
   * 支持远程调用ejb
   * @param jndiName String
   * @param url String    样式:"jnp://10.168.188.165:1099";
   * @throws Exception
   * 2005-10-20 wuqq create
   */
  public EJBAgentHelper(String jndiName,String url,String initial_context_factory) throws Exception {
//    try {
//      Context ctx1 = null;
//
//      if (url == null ){
//        ctx1 = new InitialContext();
//      }
//      else if ( url.equals("")){
//        ctx1 = new InitialContext();
//      }
//      else {
//        Hashtable env = new Hashtable();
//        env.put(Context.INITIAL_CONTEXT_FACTORY,initial_context_factory);
//        env.put(Context.PROVIDER_URL, url);
//        ctx1 = new InitialContext(env);
//      }
//      Object home = ctx1.lookup(jndiName);
//      EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(home, EJBHome.class);
//      Class ejbHomeClass = ejbHome.getClass();
//      Method method = ejbHomeClass.getDeclaredMethod("create", null);
//      remoteObject = method.invoke(ejbHome, null);
//      ejbName = jndiName;
//    }
//    catch (Exception e) {
//      throw new Exception(e.getMessage());
//    }
  }


  public Object dynaCallEjb(String methodName, ArgumentHolder ah) throws
      Exception {
//    if (!Invoker.isMethod(remoteObject, methodName, ah)) {
//      CommonOutput CommonOutput = RSUtil.setOutput( -1,
//                                             "EJB名:" + ejbName + ",方法名:" +
//                                             methodName +
//                                             ".找不到", null);
//      return CommonOutput;
//    }
//    return Invoker.dynaCall(remoteObject, methodName, ah);
      return null;
  }

  public Object dynaCallEjb(String methodName, CommonInput CommonInput) throws
      Exception {
    ArgumentHolder ah = new ArgumentHolder();
    ah.setArgument(CommonInput);
    ah.setArgument(methodName);
    return dynaCallEjb(EJB_DISPATCH_NAME, ah);
  }

//  public Object dynaCallEjb(String methodName, CommonInput CommonInput,
//                            ServletContext servletContext,
//                            HttpServletRequest httpServletRequest,
//                            String funcCode, String dataSrc, int flag) throws
//      Exception {
//    httpServletRequest.setAttribute("_FUNC_CODE", funcCode);
//    CommonOutput CommonOutput = null;
//
////    if ( (flag == 0) &&
////        CommUtil.isExistsDataCache(httpServletRequest, CommonInput, funcCode,
////                                   dataSrc)) {
////      CommonOutput = CommUtil.getDataCache(httpServletRequest, funcCode, dataSrc);
////      return CommonOutput;
////    }
//    ArgumentHolder ah = new ArgumentHolder();
//    ah.setArgument(CommonInput);
//    ah.setArgument(methodName);
//    CommonOutput = (CommonOutput) dynaCallEjb(EJB_DISPATCH_NAME, ah);
//    //对报表变量添加报表全局变量ROWSET
//    String sRetValue = CommonOutput.getRetValue();
//    if (sRetValue.length()>=6){
//      if (sRetValue.substring(0, 6).equalsIgnoreCase("report")) {
//        CommonOutput co_Ver = (CommonOutput) httpServletRequest.getSession().
//            getAttribute(Constant.
//                         USER_COMMON_VAL);
//        CommonOutput = RSUtil.addOutput2(CommonOutput, RSUtil.getRS(co_Ver, 0));
//
//      }
//    }
//
//    //从全局变量中查找某个查询结果需要用到的列，并缓存在CommonOutputExt
//    for (int i = 0; i < RSUtil.getRSNum(CommonOutput); i++) {
//      RowSet rs = RSUtil.getRS(CommonOutput, i);
//      ArrayList arr = new ArrayList();
//      for (int j = 1; j <= rs.getMetaData().getColumnCount(); j++) {
//        ArrayList a = CommUtil.getFieldDesc(servletContext,
//                                            rs.getMetaData().getColumnName(j));
//        arr.add(a);
//      }
//      RSUtil.setFieldDesc(CommonOutput, i, arr);
//    }
//    CommonOutput = CommUtil.calSumField(CommonOutput);
//    CommonOutput = CommUtil.checkHiddenField(CommonOutput);
//    CommUtil.setDataCache(httpServletRequest, CommonOutput, CommonInput, funcCode,
//                          dataSrc);
//    return CommonOutput;
//  }
}
